package servlet;

import model.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "UpdateServlet",urlPatterns = "/UpdateServlet")
public class UpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String id= (String) request.getSession().getAttribute("id");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String birth = request.getParameter("birth");
        String email=request.getParameter("email");
        String content = request.getParameter("content");
        User user=new User(id, name, sex, birth, email, content);
        System.out.println("接收到了……");
        System.out.println(user);
        UserService userService=new UserService();
        boolean b = userService.userUpdateService(user);
        if(b){
            List<User> userList=userService.userQueryAllService();

            request.getSession().setAttribute("userList",userList);
            response.sendRedirect("main.jsp");
        }else {
            request.getSession().setAttribute("errorUpdate","修改失败");
            System.out.println("修改失败");
            response.sendRedirect("update2.jsp");

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
